﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;

namespace PhotoViewer
{
    public partial class frmPicWater : Form
    {
        public frmPicWater()
        {
            InitializeComponent();
        }

        public string imagePath;
        public Image image;
        //获取image的高度和宽度
        int txtHeightt;
        int txtWidth;

        FontStyle fontStyle = FontStyle.Regular;//默认情况下的字形
        Color fontColor = Color.Red;//默认情况下是黑色
        FontFamily fontFamily = FontFamily.GenericMonospace;//设置等宽度字体（相当于word中的字体）
        float fontSize = 18;//设置字体的默认大小
        
        private void frmPicWater_Load(object sender, EventArgs e)
        {
            this.picBox.Image = image;
        }

        /// <summary>
        /// 字体设置
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFontSet_Click(object sender, EventArgs e)
        {
            fontDialog1.ShowColor = true;
            fontDialog1.ShowApply = false;  //不显示应用按钮
            fontDialog1.ShowHelp = false;   //不显示帮助按钮
            if(fontDialog1.ShowDialog()==DialogResult.OK)
            {
                fontStyle = fontDialog1.Font.Style;//设置字形（bold/italic…………）
                fontSize = fontDialog1.Font.Size;//大小
                fontColor = fontDialog1.Color;//颜色
                fontFamily = fontDialog1.Font.FontFamily;
            }
        }

        
        /// <summary>
        /// 为图片增加文字水印
        /// </summary>
        /// <param name="x"></param>
        /// <param name="y"></param>
        /// <param name="txt"></param>
        public void MakeWatermark(int x, int y, string txt)
        {
            Image image = Image.FromFile(imagePath);
            Graphics graphic= Graphics.FromImage(image);//image作为graphics的载体（即在image上绘制）
            Font font = new Font(fontFamily, fontSize, fontStyle);
            Brush brush = new SolidBrush(fontColor);

            graphic.DrawString(txt, font, brush, x, y);//绘制文本

            //SizeF 存储有序浮点数对，通常为矩形的宽度和高度
            SizeF txtSize = graphic.MeasureString(txt, font); //获得字符大小

            txtWidth = (int)txtSize.Width;
            txtHeightt = (int)txtSize.Height;
            graphic.Dispose();//graphics为非托管类，需要进行资源释放

            picBox.Image = image;
        }

        /// <summary>
        /// 预览
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnPreview_Click(object sender, EventArgs e)
        {
            picBox.Image = image;

            if (txtChar.Text.Trim() != "")
            {
                if (radioButton1.Checked)//左上
                {
                    int x = 10;
                    int y = 10;
                    MakeWatermark(x, y, txtChar.Text.Trim());
                }
                if (radioButton2.Checked)//左下
                {
                    int x1 = 10, y1 = image.Height -txtHeightt;
                    MakeWatermark(x1, y1, txtChar.Text.Trim());
                }
                if (radioButton3.Checked)//居中
                {
                    int x2 = (int)(image.Width -txtWidth) / 2;
                    int y2 = (int)(image.Height -txtHeightt) / 2;
                    MakeWatermark(x2, y2, txtChar.Text.Trim());
                }
                if (radioButton4.Checked)//右上
                {
                    int x3 = image.Width -txtWidth;
                    int y3 = 10;
                    MakeWatermark(x3, y3, txtChar.Text.Trim());
                }
                if (radioButton5.Checked)//右下
                {
                    int x4 = image.Width -txtWidth;
                    int y4 = image.Height -txtHeightt;
                    MakeWatermark(x4, y4, txtChar.Text.Trim());
                }
            }
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSave_Click_1(object sender, EventArgs e)
        {

            saveFileDialog1.Filter = "BMP|*.bmp|JPEG|*.jpeg|GIF|*.gif|PNG|*.png";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                string picPath = saveFileDialog1.FileName;//完整的图片路径
                string imageType = picPath.Substring(picPath.LastIndexOf(".") + 1, picPath.Length - picPath.LastIndexOf(".") - 1);
                imageType.ToLower();

                switch (imageType)
                {
                    case "bmp":
                        Bitmap bt = new Bitmap(picBox.Image);
                        Bitmap mybmp = new Bitmap(bt, image.Width, image.Height);
                        mybmp.Save(picPath, ImageFormat.Bmp); //保存用户所选择的路径
                        break;
                    case "jpeg":
                        Bitmap bt1 = new Bitmap(picBox.Image);
                        Bitmap mybmp1 = new Bitmap(bt1, image.Width, image.Height);
                        mybmp1.Save(picPath, ImageFormat.Jpeg);
                        break;
                    case "gif":
                        Bitmap bt2 = new Bitmap(picBox.Image);
                        Bitmap mybmp2 = new Bitmap(bt2, image.Width, image.Height);
                        mybmp2.Save(picPath, ImageFormat.Gif);
                        break;
                    case "png":
                        Bitmap bt3 = new Bitmap(picBox.Image);
                        Bitmap mybmp3 = new Bitmap(bt3, image.Width, image.Height);
                        mybmp3.Save(picPath, ImageFormat.Png);
                        break;
                }

            }
        }


    }
}

